package com.dai.chun.dao.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.dai.chun.dao.dao.EmployeeDao;
import com.dai.chun.dao.impl.DepartmentDaoImpl;
import com.dai.chun.dao.impl.EmployeeDaoImpl;
import com.dai.chun.dao.model.Department;
import com.dai.chun.dao.model.Employee;
import com.dai.chun.util.EncrypterUti;
import com.dai.chun.util.PageSplitUtil;


public class EmployeeAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private EmployeeDao employeeDao;
	private Employee employee;
	private List<Employee> employeeList;
	private PageSplitUtil psu;
	private List<Department> deptList;
	
	//添加
	public String addEmployee() throws IOException, SQLException{
		employeeDao=new EmployeeDaoImpl();
		/*if(employee.getEmp_birth()!=null){
			employee.setEmp_birth
			(new SimpleDateFormat("yyyy-MM-dd").format(employee.getEmp_birth()));
		}*/
		employee.setEmp_password(EncrypterUti.encrypt(employee.getEmp_name()));
		employeeDao.save(employee);
		return "addEmployee";
	}
	//显示增加员工页面
	public String showAddEmployee() throws IOException, SQLException{
		DepartmentDaoImpl departmentDao=new DepartmentDaoImpl();
		setDeptList(departmentDao.listDepartment());
		return "showAddEmployee";
	}
	//显示删除员工的界面
	public String showDelEmployee() throws IOException, SQLException{
		DepartmentDaoImpl departmentDao=new DepartmentDaoImpl();
		setDeptList(departmentDao.listDepartment());
		return "showDelEmployee";
	}
	
	public String listEmployee() throws IOException, SQLException{
		employeeDao=new EmployeeDaoImpl();
		if(psu==null){
			psu=new PageSplitUtil(1);
		}
		psu.comput();
		psu.computPageCount(employeeDao.findAllCount());
		employeeList=employeeDao.listEmployee(psu);
		return "listEmployee";
	}
	/*
	 * 输出<option value="用户ID">用户名</option>
	 */
	public void listEmployeeByDeptId(){
		int dept_id=Integer.parseInt(ServletActionContext.getRequest().getParameter("dept_id"));
		employeeDao=new EmployeeDaoImpl();
		List<Employee> empList=employeeDao.listEmployeeByDeptId(dept_id);
		outOptions(empList);
	}
	/**
	 * 输出<option value="用户ID">用户名</option>
	 * @param empList
	 */
	private void outOptions(List<Employee> empList) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		String data = null;
		for(Employee emp:empList){
			data+="<option value=\""+emp.getEmp_id()+"\">"+emp.getEmp_name()+"</option>";
		}
		try {
			out = response.getWriter();
			out.write(data);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

		
	}
	//删除
	public String delEmployee() throws SQLException{
		employeeDao=new EmployeeDaoImpl();
		employeeDao.delEmployee(employee.getEmp_id());
		return "delEmployee";
	}
	//修改密码
	public String changePassword() throws SQLException{
		employeeDao=new EmployeeDaoImpl();
		Employee emp=employeeDao.findEmployeeById(employee.getEmp_id());
		if(EncrypterUti.verify(emp.getEmp_password(),employee.getEmp_password() )){
			employee.setEmp_password(EncrypterUti.encrypt(employee.getEmp_repassword()));
			employeeDao.changePassword(employee);
			return "changePassword";
		}
		return "fail";
		
	}
	//查找
	public String searchEmployee() throws SQLException{
		String sql = "select * from t_employee where";
	if (!employee.getEmp_num().equals("")) {// 即使提交的全是空，也会执行

		sql += " emp_num=" + employee.getEmp_num();

	} else if (0!= employee.getEmp_dept_id()) {

		sql += "  emp_dept_id='" + employee.getEmp_dept_id();
	}else if(!employee.getEmp_name().equals("")){
		sql += "  emp_name='" + employee.getEmp_name();
	}
	if(sql.endsWith("where")){
		sql=sql.substring(0, sql.indexOf("where"));
	}
	sql = sql + ";";
	//logger.info("searchWage"+sql);
	employeeDao=new EmployeeDaoImpl();
	employeeList=employeeDao.searchWage(sql);
	return "searchEmployee";
		
	}
	
	//修改
	public String updateEmployee() throws SQLException{
		employeeDao=new EmployeeDaoImpl();
		employeeDao.updateEmployee(employee);
		return "updateEmployee";
	}
	//显示修改员工的页面
	public String showUpdateEmployee() throws SQLException{
		employeeDao=new EmployeeDaoImpl();
		employee=employeeDao.findEmployeeById(employee.getEmp_id());
		return "showUpdateEmployee";
	}
	
	public List<Employee> getEmployeeList() {
		return employeeList;
	}
	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}
	
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public PageSplitUtil getPsu() {
		return psu;
	}

	public void setPsu(PageSplitUtil psu) {
		this.psu = psu;
	}
	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

}
